package win.demonlegion.util;

import com.alibaba.fastjson.JSON;
import win.demonlegion.common.HttpCode;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by MK on 2017/6/18.
 * 返回结果的JSON字符串
 */
public class ResultUtil {

    public static String getResult(Object object) {
        return JSON.toJSONString(object);
    }

    public static String getResult(String name, Object object) {
        Map<String, Object> map = new HashMap<>();
        map.put(name, object);

        return JSON.toJSONString(map);
    }

    public static String getResult(HttpCode code) {
        return getResult(code, null, null);
    }

    public static String getResult(HttpCode code, String name, Object object) {
        Map<String, Object> map = new HashMap<>();
        if(code != null) {
            map.put("code", code.getValue());
            map.put("message", code.getMessage());
        }
        if(object != null) map.put(name, object);
        return JSON.toJSONString(map);
    }
}
